using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;

using SypEngine.Garbage;
using SypEngine.Middleware;

namespace SypEngine.SiteConfig
{
    public class SypSiteHandler : SypObject
    {
        public List<SypSite> Sites = null;


        public void RemoveSite(string SiteId)
        {
            SypSite SConf = GetSiteConfig(SiteId);
            if (SConf != null)
            {
                //Remove Local Site Copy
                if (Directory.Exists(SypGarbage.TMP_FOLDER + SiteId))
                    Directory.Delete(SypGarbage.TMP_FOLDER + SiteId, true);

                //Remove Ftp Site



                //Remove Conf File
                if (File.Exists(SiteId + SypGarbage.CONF_EXTENSION))
                    File.Delete(SiteId + SypGarbage.CONF_EXTENSION);
            }
        }

        public SypSiteHandler(SetText OutputMethod)
            : base(OutputMethod)
        {
            if (Sites == null)
                this.Sites = new List<SypSite>();
            if (VisitorList != null)
                VisitorList = new List<SypVisitor>();

            VisitorList.Add(new SypAlbumVisitor(OutputMethod));
            VisitorList.Add(new SypSectionVisitor(OutputMethod));
            VisitorList.Add(new SypPictureVisitor(OutputMethod));
            VisitorList.Add(new SypSiteVisitor(OutputMethod));
        }


        public void SerializeToXml(string destinationfile, SypSite SiteConf)
        {
            try
            {
                XmlSerializer MySerializer = new XmlSerializer(typeof(SypSite), new Type[] { typeof(List<Album>), typeof(List<Section>), typeof(Album), typeof(Section), typeof(FTPConf) });
                StreamWriter MyWriter = new StreamWriter(destinationfile, false, System.Text.Encoding.UTF8);
                MySerializer.Serialize(MyWriter, SiteConf);
                MyWriter.Close();
            }
            catch (Exception)
            {
                OutputMethod("Unexpected error while processing serialization of : " + destinationfile, Output.ERROR);
            }
        }


        public SypSite LoadFromXml(string sourcefile)
        {
            try
            {
                XmlSerializer MySerializer = new XmlSerializer(typeof(SypSite), new Type[] { typeof(List<Album>), typeof(List<Section>), typeof(Album), typeof(Section), typeof(FTPConf) });
                FileStream MyStream = new FileStream(sourcefile, FileMode.Open);
                SypSite SiteConfig = (SypSite)MySerializer.Deserialize(MyStream);
                MyStream.Close();
                return SiteConfig;
            }
            catch (Exception)
            {
                OutputMethod("Unexpected error while processing unserialization of : " + sourcefile, Output.ERROR);
                return null;
            }
        }


        public void SerializeSites()
        {
            foreach (SypSite Sconf in Sites)
                this.SerializeToXml(SypGarbage.SITES_CONF + Sconf.SiteId + SypGarbage.CONF_EXTENSION, Sconf);
        }

        public void SerializeSite(int sectId)
        {
            SypSite mySite = GetSiteConfig(sectId);
            if (mySite != null)
                SerializeToXml(SypGarbage.SITES_CONF, mySite);    
        }

        public void CheckAvailableSites()
        {
            string[] SiteConfigFiles = null;
            if (Directory.Exists(SypGarbage.SITES_CONF))
                SiteConfigFiles = System.IO.Directory.GetFiles(SypGarbage.SITES_CONF, "*" + SypGarbage.CONF_EXTENSION);
            else
                SiteConfigFiles = null;

            if (SiteConfigFiles != null)
            {
                foreach (string SiteConf in SiteConfigFiles)
                {
                    SypSite Sconf = this.LoadFromXml(SiteConf);
                    if (SypGarbage.DEBUG)
                        OutputMethod("Load SiteConf : " + Sconf.SiteTitle, Output.OUT);
                    if (Sconf != null)
                    {
                        Sites.Add(Sconf);
                    }
                }
            }
        }


        public SypSite GetSiteConfig(string SiteName)
        {
            foreach (SypSite site in Sites)
            {
                if (site.SiteTitle.Equals(SiteName))
                    return site;
            }
            return null;
        }

        public SypSite GetSiteConfig(int SiteId)
        {
            foreach (SypSite site in Sites)
            {
                if (site.SiteId == SiteId)
                    return site;
            }
            return null;
        }


        public void AddSite(SypSite Sconf)
        {
            if (Sites == null)
                Sites = new List<SypSite>();
            Sites.Add(Sconf);
        }
    }

}
